<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-row type="flex" justify="end">
        <el-button size="small" type="primary" @click="addPerm(1, '0')">添加页面权限</el-button>
      </el-row>
      <!-- el-table 本来就可以显示树形数据, 只要行数据中有 children 即可
      唯一需要注意的是, 如果要显示树形, 必须制定每行对象中都有的一个独一无二属性
      作为 row-key 才会出现 -->
      <el-table
        :data="permList"
        row-key="id"
        :default-expand-all="true"
      >
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="obj">
            <el-button v-if="obj.row.type === 1" type="text" @click="addPerm(2,obj.row.id)">新增子权限</el-button>
            <el-button type="text" @click="editPerm(obj.row.id)">编辑权限</el-button>
            <el-button type="text" @click="delPerm(obj.row.id)">删除权限</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :title="formData.id?'编辑权限':'新增权限'" :visible="isShowDialog" @close="btnCancel">
        <el-form ref="permForm" :model="formData" :rules="rules" label-width="88px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="是否激活">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermList, getPermDetail, editPerm, delPerm, addPerm } from '@/api/permission'
import { listToTreeData } from '@/utils'
export default {
  data() {
    return {
      permList: [],
      isShowDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '1'
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadPage()
  },
  methods: {
    async loadPage() {
      // 因为权限描述间, 有上下级树形关系, 这里训词将 id + pid 的数据结构
      // 转成element ui 可以渲染的树形数据
      this.permList = listToTreeData(await getPermList(), '0')
      console.log(this.permList)
    },
    addPerm(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.isShowDialog = true
    },
    async editPerm(id) {
      this.formData = await getPermDetail(id)
      this.isShowDialog = true
    },
    async btnOK() {
      // 校验表单
      await this.$refs.permForm.validate()
      // 发送请求 根据有没有 id 决定发送编辑还是新增
      if (this.formData.id) {
        await editPerm(this.formData)
      } else {
        await addPerm(this.formData)
      }
      // 提示用户
      this.$message.success('操作成功')
      // 更新页面
      this.loadPage()
      // 关闭弹窗
      this.isShowDialog = false
    },
    btnCancel() {
      // 清理数据
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '1'
      }
      // 清理报错
      this.$refs.permForm.resetFields()
      // 关闭弹窗
      this.isShowDialog = false
    },
    async delPerm(id) {
      try {
        // 二次询问
        await this.$confirm('是否确认删除')
        // 发送请求
        await delPerm(id)
        // 提示用户
        this.$message.success('操作成功')
        // 更新页面
        this.loadPage()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
